/*timer.c -- Initialises the PIT, and handles clock updates.*/
/*Written for JamesM's kernel development tutorials.*/

#include "timer.h"
#include "isr.h"
#include "monitor.h"

u32int tick = 0;

static void timer_callback()
{
   tick++;
   /*if (tick<10)
   {
       monitor_write("Tick: ");
       monitor_write_dec(tick);
       monitor_write("\n");
   }*/
}

void init_timer(u32int frequency)
{
   u32int divisor;
   u8int l, h;
   /*Firstly, register our timer callback.*/
   register_interrupt_handler(IRQ0, &timer_callback);

   /*The value we send to the PIT is the value to divide it's input clock
   (1193180 Hz) by, to get our required frequency. Important to note is
   that the divisor must be small enough to fit into 16-bits.
   Divisor has to be sent byte-wise, so split here into upper/lower bytes.*/
   divisor = 1193180 / frequency;
   l = (u8int)(divisor & 0xFF);
   h = (u8int)( (divisor>>8) & 0xFF );

   /*Send the command byte.*/
   outb(0x43, 0x36);

   /*Send the frequency divisor.*/
   outb(0x40, l);
   outb(0x40, h);
} 

